// 企业信息
import { getToken, setToken, removeToken } from "@/util/auth";
import url from "../../../config/url";
import axios from "axios";
axios.defaults.baseURL = url.apiUrl;
axios.interceptors.request.use(
  function(config) {
    config.headers.token = localStorage.getItem("usertoken");
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);
import listApi from "../../../api/enterprise";
const enterprisecar = {
  namespaced: true,
  state: {
    list: [], //数据列表
    params: {
      //查询参数
      data: ""
    },
    pageSize: "10",
    pageNum: 1,
    loading: false, //加载中...控制器
    finished: false, //所有数据加载完成
    finishedText: "数据全部加载完成", //所有数据加载完成
    title: "企业用车", //页面标题栏，可以不写，标题栏会以路由中的title为默认
    dataStatus: "-1", //状态 0:数据为空;1:网络错误 -1:不显示(默认)
    updateCorpStatus: {
      //添加，修改企业状态
      text: "",
      data: {}
    }
  },

  mutations: {
    mutFinished(state, payload) {
      //控制fininsh状态
      state.finished = payload.finished;
    },
    mutLoading(state, payload) {
      //控制loading状态
      state.loading = payload.loading;
    },
    mutList(state, payload) {
      //数据展示列表 payload:{data:数据，search:是否是搜索}
      state.list = state.list.concat(payload.data);
    },
    mutClearList(state) {
      //清空列表
      state.list = [];
      state.finished = false;
      state.params = { data: "", text: "" };
    },
    mutParams(state, payload) {
      //查询参数
      state.params.data = payload.data;
    },
    muUpdateCorpStatus(state, payload) {
      //查询参数
      state.updateCorpStatus.data = payload;
    },

    mutDataStatus(state, payload) {
      //列表数据状态
      state.dataStatus = payload.status;
    },
    mutReset(state) {
      state.loading = false;
      state.finished = false;
      state.dataStatus = "-1";
      state.pageNum = 1;
    }
  },

  actions: {
    async actList(ctx) {
      //获取公司列表
      ctx.state.loading = true;
      await axios
        .get(
          listApi.findCorpCarList +
            "?pageSize=" +
            ctx.state.pageSize +
            "&pageNum=" +
            ctx.state.pageNum +
            "&corpId=" +
            ctx.state.params.data
        )
        .then(response => {
          let res = response.data;
          if (res.code === 200) {
            if(typeof res.data =="string"){
              ctx.state.updateCorpStatus.text = res.message
              ctx.commit("mutLoading", {
                loading: false
              });
              ctx.commit("mutFinished", {
                finished: true
              });
              ctx.commit("mutDataStatus", {
                status: "0"
              });
              return null;
            }
            if (
              res.data.records == undefined ||
             ( res.data.records.length == "0" &&
              ctx.state.list.length == "0")
            ) {
              //数据为空
              ctx.commit("mutDataStatus", {
                status: "0"
              });
              ctx.commit("mutFinished", {
                finished: true
              });
              return;
            }
            ctx.commit("mutList", {
              data: res.data.records
            });
            if (res.data.total == ctx.state.list.length) {
              //数据加载完
              ctx.commit("mutFinished", {
                finished: true
              });
              return;
            }
            ctx.state.pageNum++;
          } else {
            ctx.commit("mutLoading", {
              loading: false
            });
            ctx.commit("mutFinished", {
              finished: true
            });
            ctx.commit("mutDataStatus", {
              status: "0"
            });
          }
        })
        .catch(error => {
          ctx.commit("mutLoading", {
            loading: false
          });
          ctx.commit("mutFinished", {
            finished: true
          });
          ctx.commit("mutDataStatus", {
            status: "1"
          });
        });
    },
    actUpdateCarInfo(ctx) {
      //修改信息
      return axios
        .post(
          listApi.updateCarInfo,
          {
            carId: ctx.state.updateCorpStatus.data.carId,
            czqxQ: ctx.state.updateCorpStatus.data.czqx.split("至")[0],
            czqxZ: ctx.state.updateCorpStatus.data.czqx.split("至")[1]
          },
          {
            token: localStorage.getItem("usertoken")
          }
        )
        .then(response => {
          ctx.state.updateCorpStatus.text = response.data.message;
        })
        .catch(error => {
          ctx.state.updateCorpStatus.text = error;
        });
    }
  },
  getters: {
    //get获取数据
    getDataStatus(state) {
      return state.dataStatus;
    },
    getLoading(state) {
      return state.loading;
    },

    getFinished(state) {
      return state.finished;
    },
    getFinishedText(state) {
      return state.finishedText;
    },
    getTitle(state) {
      return state.title;
    },
    getIsSearch(state) {
      //是否是查询标记
      return state.isSearch;
    },
    getList(state) {
      //获取列表
      return state.list;
    },
    getUpdateCorpStatus(state) {
      //添加修改企业状态
      return state.updateCorpStatus;
    }
  }
};

export default enterprisecar;
